package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 完善个人信息
     *
     * @param param
     * @param token
     * @return
     */
    @RequestMapping("loginReginfo")
    @PostMapping
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String, String> param, @RequestHeader("Authorization") String token) {

        try {
            //获取前端传入的token数据和用户填写的信息
            Boolean saveUserInfo = this.userInfoService.saveUserInfo(param, token);
            if (saveUserInfo) {
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000000").errMessage("发生错误").build();

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
     *
     *   选取头像操作
     * @param file  用户传入的图片
     * @param token token数据
     * @return
     */
    @PostMapping
    @RequestMapping("loginReginfo/head")
    public  ResponseEntity<Object> saveLogo(@RequestParam("headPhoto") MultipartFile file,@RequestHeader("Authorization") String token){

        try {
            //将用户传入的图片及token传给service处理
            Boolean savelogo = this.userInfoService.savelogo(file, token);
            //判断用户数据是否存在
            if (savelogo){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //创建错误信息 ,如果无用户信息则 提示出错
        ErrorResult errorResult = ErrorResult.builder().errCode("000000").errMessage("图片非人像,请重新上传").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

}